package com.cherrish;

import io.grpc.Channel;
import net.devh.springboot.autoconfigure.grpc.client.GrpcClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author cherrish
 * @time 2018-09-03 17:10
 * @name UserGrpcService
 * @desc:
 */
@Service
public class UserGrpcService {
    private Logger logger = LoggerFactory.getLogger(UserGrpcService.class);
    @GrpcClient("user-grpc-server")
    private Channel serverChannel;

    public String addUser( ){
        UserServiceGrpc.UserServiceBlockingStub stub = UserServiceGrpc.newBlockingStub(serverChannel);
        BooleanReply response = stub.addUser(
                User.newBuilder()
                        .setId("10001")
                        .setUsername("grpc-test-user")
                        .setAddress("SHENZHEN")
                        .setAge(18)
                        .setSex(1)
                        .setCreatetime(System.currentTimeMillis() + "")
                        .setUpdatetime(System.currentTimeMillis() + "")
                        .build());
        logger.info("grpc-client received: ---> "+response.getReply());
        if(response.getReply()){
            return "success";
        }else{
            return "fail";
        }
    }
}

